package com.acme.bankapp;

import java.io.*;

/**
 * Created by phashik on 21.11.2014.
 */
public class ClientConnection {
    InputStream inputStream;
    OutputStream outputStream;
    private boolean sessionEnded = false;

    public ClientConnection(InputStream inputStream, OutputStream outputStream) {
        this.inputStream = inputStream;
        this.outputStream = outputStream;
    }

    public void start() {
        try (
                DataInputStream in = new DataInputStream(inputStream);
                DataOutputStream out = new DataOutputStream(outputStream);
                BufferedReader kbdIn = new BufferedReader(new InputStreamReader(System.in));
        ) {
            String command;
            while (!sessionEnded) {
                command = kbdIn.readLine();
                if ("exit".equals(command)) {
                    sessionEnded = true;
                }
                processCommand(command, in, out);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void processCommand(String command, DataInputStream in, DataOutputStream out) throws IOException {
        out.writeUTF(command);
        int respondLinesNumber = in.readInt();
        for (int i = 0; i < respondLinesNumber; i++) {
            System.out.println(in.readUTF());
        }
    }
}
